package cn.lixu.org.controller;

import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.PageList;
import cn.lixu.org.domain.Employee;
import cn.lixu.org.query.EmployeeQuery;
import cn.lixu.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService EmployeeService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Employee> loadAll() {
        return EmployeeService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id) {
        return EmployeeService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            EmployeeService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param Employee
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Employee Employee) {
        try {
            if (Employee.getId() == null) {
                EmployeeService.save(Employee);
            } else {
                EmployeeService.update(Employee);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param EmployeeQuery
     * @return
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery EmployeeQuery) {
        return EmployeeService.queryPage(EmployeeQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            EmployeeService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 根据页面传输的shopId查询所有的店铺员工
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "根据shopid查询" )
    public List<Employee> findByShopId(@PathVariable("shopId") Long shopId) {

        return EmployeeService.findByShopId(shopId);

    }


}

